<?php

class Admin_users_model extends CI_Model{

	function validate(){
		$return = null;
		$this->db->where('email', $this->input->post('email', true));
		$pass = $this->input->post('password', true);
		$this->db->where('password', sha1(md5($pass)));
		$this->db->where('type', 'admin', true); // admin: 1
		$query = $this->db->get('mlme_users');
		if( $query->num_rows == 1 ){
			$user = $query->result();
			$userid = $user[0]->userid;
			$return = array('validated'=>true, 'userid'=>$userid);
		}
		return $return;
	}
	//count total rows
	function listUsers(){
		$qstr = $this->_buildQstr();
		$query = $this->db->query($qstr);
		return $query->num_rows;
	}
	//to be used in pagination
	function listUsers_limited( $start_row=0, $limit=100 ){
		$qstr = $this->_buildQstr();
		$qstr .= " LIMIT $start_row, $limit";
		$query = $this->db->query($qstr);
		return ($query->num_rows() > 0) ? $query->result() : false;
	}
	//returns query string
	function _buildQstr(){
		$filter = @$_SESSION['search_text'];
		$key = $this->Quote('%'.mysql_real_escape_string( trim( strtolower( $filter ) ) ).'%');
		if($filter){		//if search is not empty
			$qstr = "SELECT *
						FROM (
						`mlme_users` tbl
						)
						WHERE (LOWER( tbl.email ) LIKE $key
						OR LOWER( tbl.firstname ) LIKE $key
						OR LOWER( tbl.lastname ) LIKE $key
						OR LOWER( tbl.middle ) LIKE $key)
						AND (type='free' OR type='vip')";
		} else {
			$qstr = "SELECT *
						FROM (
						`mlme_users` tbl
						)
						WHERE tbl.type='free' OR tbl.type='vip'";
		}
		return $qstr;
	}
	/**
	* Get a quoted database escaped string
	*
	* @param	string	A string
	* @param	boolean	Default true to escape string, false to leave the string unchanged
	* @return	string
	* @access public
	*/
	function Quote( $text, $escaped = true )
	{
		return '\''.($escaped ? mysql_real_escape_string( $text ) : $text).'\'';
	}
	//get the user data
	//@param id: userid
	function getUser( $id=null ){
		return $this->db->get_where('mlme_users', array('userid' => $id ));
	}
	//insert new data
	function addUser( $data ){
		$data['date_added'] = $data['date_updated'] = date('Y-m-d h:i:s');
		$data['password'] = sha1(md5($data['password']));
		//we need to unset the cpassword
		unset($data['cpassword']);
		return $this->db->insert('mlme_users', $data);
	}
	//update existing data
	function updateUser( $id, $data ){
		$data['date_updated'] = date('Y-m-d h:i:s');
		//we need to unset the cpassword
		unset($data['cpassword']);
		$this->db->where('userid', $id);
		return $this->db->update('mlme_users', $data);
	}
	//delete data
	function deleteUser( $id=array() ){
		if( empty($id) ) return false;
		$this->db->where_in( 'userid', $id );
		return $this->db->delete('mlme_users');
	}
	function checkEmailEdit($data){
		$qry = 'SELECT * FROM `mlme_users` WHERE `email`="'.$data['email'].'" AND `userid`!="'.$data['userid'].'"';
		$query = $this->db->query($qry);
		return ($query->num_rows() > 0) ? $query->result() : false;

	}
	function update_user_status($userid,$status){
		$data = array('status'=>$status,'date_updated'=>date('Y-m-d H:i:s'));
		$this->db->where('userid',$userid);
		return $this->db->update('mlme_users',$data);
	}//end fct

	/*	function search($keyword_data){
	$keyword=$keyword_data;
	$columns = '* ';
	$this->db->select($columns);
	$this->db->from('mlme_users');
		if(isset($keyword_data)&&(!empty($keyword_data))){
		  $explodedKeyword = explode(' ',$keyword_data);
		   if(isset($keyword) && !empty($keyword)) $this->db->or_where_in('firstname',$explodedKeyword);
		   if(isset($keyword) && !empty($keyword)) $this->db->or_where_in('lastname', $explodedKeyword);
		   if(isset($keyword) && !empty($keyword)) $this->db->or_where_in('lastname', $explodedKeyword);
		   if(isset($keyword) && !empty($keyword)) $this->db->or_where_in('email', $explodedKeyword);
		}
	$this->db->where('type','vip');
	$this->db->or_where('type','free');
    $this->db->group_by('userid');
	$query = $this->db->get();
	return ($query->num_rows() > 0) ? $query->result() : FALSE ;
	}*/
}